/**
 * Copyright (c) 2013 onionframework.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.onionframework.core.optional;

import java.io.IOException;

import org.onionframework.core.config.SystemConfig;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

/**
 * 对于使用Annotation定义的Optional的bean，在这里过滤，决定是否加载
 * 
 * @author allan
 *
 */
public class OptionalAnnotationBeanFilter implements TypeFilter {

	@Override
	public boolean match(MetadataReader metadataReader,
			MetadataReaderFactory metadataReaderFactory) throws IOException {
		if (metadataReader.getAnnotationMetadata().getAnnotationTypes()
				.contains(Optional.class.getName())) {
			String optionalPart = (String) metadataReader
					.getAnnotationMetadata()
					.getAnnotationAttributes(Optional.class.getName())
					.get("value");
			if (SystemConfig.Config.getBoolean(optionalPart, false))
				return false;
			else
				return true;
		}
		
		return false;
	}

}
